#ifndef BVH_H
#define BVH_H

#include "shape.h"
#include "ray.h"
#include "vector.h"
#include <QObject>
#include <string>

//POD struct node
class Node
{
public:
    enum NodeType
    {
        Union_NodeType,
        Intersection_NodeType,
        Substract_NodeType,
        Shape_NodeType
    } typedef Operation;

private:
    Operation _operation;
    const Shape*  _shape;
    const Node* _right;
    const Node* _left;

    Node(const Node&);

public:
    explicit Node(const Shape* shape);
    explicit Node(Operation operation, const Node* right, const Node* left);
    ~Node();

    inline bool isLeaf() const {
        return(_operation == Node::Shape_NodeType);
    }

    bool intersect(const Shape **shape,
                          const Ray& ray,
                          double *distance,
                          Vector* normal) const;
    bool inside(const Vector& dot) const;

    const Node* right() const;
    const Node* left() const;
    const Shape* shape() const;
    Operation operation() const;

    void setRight(const Node* value);
    void setLeft(const Node* value);
    void setOperation(Operation operation);
    void setShape(const Shape* value);
};

class CSG
{
    const Node* _root;
public:
    explicit CSG(Node* root);
    const Node* root() const;
    void setRoot(const Node *root);
    ~CSG();
};

#endif // CSG_H
